package antlr4.definitive.d4_expr.d4_3_0_extractinterface.run;

import antlr4.definitive.d4_expr.d4_3_0_extractinterface.gen.JavaBaseListener;
import antlr4.definitive.d4_expr.d4_3_0_extractinterface.gen.JavaParser;
import org.antlr.v4.runtime.TokenStream;

public class ExtractInterfaceListener extends JavaBaseListener {

    JavaParser parser;

    public ExtractInterfaceListener(JavaParser parser) {
        this.parser = parser;
    }



    @Override
    public void enterImportDeclaration(JavaParser.ImportDeclarationContext ctx) {
        TokenStream tokens = parser.getTokenStream();
        String importText = tokens.getText(ctx);
        System.out.println(importText);
    }


    /**
     * 监听对类定义的匹配
     */
    @Override
    public void enterClassDeclaration(JavaParser.ClassDeclarationContext ctx) {
        System.out.println();
        System.out.println("interface I" + ctx.Identifier() + "{");
    }

    @Override
    public void exitClassDeclaration(JavaParser.ClassDeclarationContext ctx) {
        System.out.println("}");
    }

    /**
     * 监听对方法定义的匹配
     */
    @Override
    public void enterMethodDeclaration(JavaParser.MethodDeclarationContext ctx) {
        //需要从语法分析器中获取词法符号
        TokenStream tokens = parser.getTokenStream();
        String type = "void";
        if (ctx.typeSpec() != null) {//方法返回值
            type = tokens.getText(ctx.typeSpec());
        }
        String args = tokens.getText(ctx.formalParameters());
        System.out.println("\t" + type + " " + ctx.Identifier() + args + ";");
    }

}
